package com.mobilesafe.engine;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.mobilesafe.domain.ContactInfo;

public class ContactInfoService {
	private static final String TAG = "ContactInfoService";
	private Context context;

	public ContactInfoService(Context context) {
		this.context = context;
	}

	/**
	 * 获得联系人信息
	 * 
	 * @return
	 */
	public List<ContactInfo> getContactInfos() {
		ContentResolver resolver = context.getContentResolver();
		// 1.获取联系人的id
		// 2.根据联系人的id 获取联系人名字
		// 3.根据联系人的id 数据的type 获取到对应的数据(电话,email);
		List<ContactInfo> infos = new ArrayList<ContactInfo>();
		ContactInfo info ;
		//"content://com.android.contacts/raw_contacts"联系人
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Uri datauri = Uri.parse("content://com.android.contacts/data");
		Cursor cursor = resolver.query(uri, null, null, null, null);
		while (cursor.moveToNext()) {
			  info = new ContactInfo();
			   String id =	cursor.getString(cursor.getColumnIndex("_id"));
			   Log.i(TAG, "联系人ID："+id);
			   String name =cursor.getString(cursor.getColumnIndex("display_name"));
			   info.setName(name);
			   Cursor datacursor =  resolver.query(datauri, null, "raw_contact_id=?", new String[]{id}, null);
			   while (datacursor.moveToNext()) {
				//mimetype
				String type = datacursor.getString(datacursor.getColumnIndex("mimetype"));
				//"vnd.android.cursor.item/phone_v2"这个Url代表着电话
				if("vnd.android.cursor.item/phone_v2".equals(type)){
					String number = datacursor.getString(datacursor.getColumnIndex("data1"));
					info.setPhone(number);
					Log.i(TAG, "手机号码："+number);
				}
			}
			   datacursor.close();
			   infos.add(info);
			   info = null;
			}
			cursor.close();
			return infos;
		
	}

}
